package com.neuedu.datatype;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neuedu.bean.TUser;
import com.neuedu.util.SqlUtil;
import com.neuedu.util.String2Date;

public class DataExample {

	public static void main(String[] args) {
		/*//日期使用字符串的形式输入
		String datestr="1996-04-12";
        //转为日期--SimpleDateFormat--java.util.Date--最终要转为java.sql.Date
		TUser use=new TUser();
		use.setUid(6);
		use.setUbirth(String2Date.getStringToDate(datestr));
		int i=updateDate(use);
		System.out.println("影响的行数为："+i);*/
		
		//提取日期
		TUser use=selectDate(6);
		System.out.println(use.getUbirth());
		
	}
	//时间字段进行提取--查找select功能--部分查找：查找一条记录
	public static TUser selectDate(int uid) {
		TUser use=new TUser();
		Connection conn= null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		
		try {
			conn=SqlUtil.getConn();
			String sql="select u_birth from t_use where u_id=?";
			pstm=conn.prepareStatement(sql);
			pstm.setObject(1, uid);
			rs=pstm.executeQuery();
			while(rs.next()) {
				use.setUbirth(rs.getDate("u_birth"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return use;
	}
	
	
	
	
	
	//时间进行了插入
	public static int updateDate(TUser use) {
		int i=-1;
		
		Connection conn=null;
		PreparedStatement pstm=null;
		
		
		try {
			conn=SqlUtil.getConn();
			String sql="upate t_use set u_birth=? where u_id=?";
			pstm=conn.prepareStatement(sql);
			pstm.setObject(1, use.getUbirth());
			pstm.setObject(2, use.getUid()); 
			
			i=pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			SqlUtil.close(pstm,conn);
		}
		
		
		return i;
	}

}
